<template>
	<view class="login-page">
		<view class="logo-box">
			<image class="logo-image" src="/static/img/logo.jpg"></image>
			<view class="logo-title">九曜健康</view>
		</view>
		<view class="login-form">
			<view class="form-item">
				<view class="item-label">
					<image class="label-icon" src="/static/img/renwu.png"></image>
					<view>账号:</view>
				</view>
				<input type="text" class="item-input" v-model="mobile" placeholder="请输入手机号" />
			</view>
			<button class="login-btn" @click="$u.throttle(userAuthorizeLogin, 5000)">登 录 系 统</button>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				mobile: '', //手机号码18820268653
				code: '',
				isWeixin: false,
			};
		},
		onShow() {
			this.isWeixin = this.isWechat()
			if (this.isWeixin) {
				this.checkWeChatCode();
			}
		},
		methods: {
			/*微信登录相关  start*/
			//方法：用来判断是否是微信内置的浏览器
			isWechat() {
				return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
			},
			//方法:用来提取code
			getUrlCode(name) {
				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||
					[, ''])[1]
					.replace(/\+/g, '%20')) || null
			},
			//检查浏览器地址栏中微信接口返回的code
			checkWeChatCode() {
				let code = this.getUrlCode('code')
				this.code = code;
				if (!code) {
					this.getWeChatCode();
				}
			},
			userAuthorizeLogin() {
				if (this.isWeixin) {
					this.userLogin(this.code);
				} else {
					this.userLogin("");
				}
			},
			//请求微信接口，用来获取code
			getWeChatCode() {
				let local = encodeURIComponent(window.location.href); //获取当前页面地址作为回调地址
				let appid = 'wx6855328a5d4996db'
				window.location.href =
					"https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
					appid +
					"&redirect_uri=" +
					local +
					"&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect";
			},
			//当前登录按钮操作
			userLogin(value) {
				//验证登录账户
				if (!this.mobile) {
					return this.$Toast('请输入您的账号');
					
				}
				//手机号密码登录
				this.$Api.common.userLogin({
					mobile: this.mobile,
					code: value,
				}).then(res => {
					this.$store.commit('UPDATE_USERINFO', res.data.userinfo)
					this.$store.commit('LOGIN', {
						token: res.data.userinfo.token
					})
					// http://localhost:8081/?code=0210DFFa1DA5dK0sI7Ia1TUD0P20DFFT&state=STATE#/
					//重定向到首页
					window.location.href = '/'
				})
			}
		}
	};
</script>

<style lang="scss">
	.login-page {
		box-sizing: border-box;
		min-height: 100vh;
		padding: 30rpx;
		background-color: #ffffff;

		.logo-box {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.logo-image {
				width: 160rpx;
				height: 160rpx;
				margin-top: 80rpx
			}

			.logo-title {
				margin-top: 40rpx;
				margin-bottom: 80rpx;
			}
		}
	}

	.form-item {
		border: 1rpx solid #eeeeee;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #ffffff;
		margin-bottom: 30rpx;
		border-radius: 10rpx;
		box-sizing: border-box;
		padding: 10rpx 30rpx;
		display: flex;
		align-items: center;

		.item-label {
			width: 140rpx;
			display: flex;
			align-items: center;
			gap: 10rpx;

			.label-icon {
				width: 26rpx;
				height: 26rpx;
			}
		}

		.item-input {
			flex: 1;
		}
	}

	.login-btn {
		margin-bottom: 20rpx;
		background: #009591;
		color: #ffffff;
	}
</style>